package 开始的剑指offer8月.双指针;

import 刷题7月.链表反转递归.ReverseList;
// https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
public class 剑指Offer22链表中倒数第k个节点0902 {

     public static class ListNode {
         int val;
         ListNode next;
         ListNode(int x) { val = x; }
     }
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode former = head, latter = head;
        for(int i = 0; i < k; i++)
            former = former.next;
        while(former != null) {
            former = former.next;
            latter = latter.next;
        }
        return latter;
    }
    public static void main(String[] args) {
        ListNode node5 = new ListNode(5);
        ListNode node4 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        ListNode node2 = new ListNode(2);
        ListNode node1 = new ListNode(1);
        node5.next=node4;
        node4.next=node3;
        node3.next=node2;
        node2.next=node1;
        node1.next=null;
        System.out.println(new 剑指Offer22链表中倒数第k个节点0902().getKthFromEnd(node5,1).val);
    }

}
